<template>
  <div class="login_container">
    <el-row>
      <el-col :span="12" :xs="0" class="login_form_left"></el-col>
      <el-col :span="12" :xs="24">
        <el-form
          class="login_form"
          :model="loginForm"
          :rules="rules"
          ref="loginForms"
          label-width="100px"
        >
          <h1>Hello</h1>
          <h2>欢迎登录XXXX平台</h2>
          <el-form-item prop="username">
            <el-input
              :prefix-icon="User"
              v-model="loginForm.username"
            ></el-input>
          </el-form-item>
          <el-form-item prop="password">
            <el-input
              type="password"
              :prefix-icon="Lock"
              :show-password="true"
              v-model="loginForm.password"
            ></el-input>
          </el-form-item>
          <el-form-item>
            <el-button
              type="primary"
              :loading="loading"
              size="default"
              class="login_btn"
              @click="login"
              >登录</el-button
            >
          </el-form-item>
        </el-form>
      </el-col>
    </el-row>
  </div>
</template>

<script lang="ts" setup>
import { User, Lock } from "@element-plus/icons-vue";
import { reactive, ref } from "vue";
import { useRouter, useRoute } from "vue-router";
import { ElNotification } from "element-plus";
import useUserStore from "@/store/modules/user";
let loginForms = ref();
const $router = useRouter();
const $route = useRoute();
let loading = ref(false);
let useStore = useUserStore();
let loginForm = reactive({
  username: "admin",
  password: "atguigu123",
});
const login = async () => {
  await loginForms.value.validate();

  loading.value = true;
  try {
    await useStore.userLogin(loginForm);
    let redirect: any = $route.query.redirect;
    $router.push({ path: redirect || "/" });

    ElNotification({
      title: `Hi,${getTime()}好`,
      message: "登录成功",
      type: "success",
      duration: 2000,
    });
  } catch (error) {
    ElNotification({
      title: "登录失败",
      message: error.message,
      type: "error",
      duration: 2000,
    });
  } finally {
    loading.value = false;
    // 登录完毕后，清空表单
    loginForm.username = "";
    loginForm.password = "";
  }
};
const rules = {
  username: [
    {
      required: true,
      message: "请输入用户名",
      trigger: "blur",
    },
  ],
  password: [
    {
      required: true,
      message: "请输入密码",
      trigger: "blur",
    },
  ],
};
const getTime = () => {
  let hours = new Date().getHours();
  let message = "";
  if (hours < 12) {
    message = "早上";
  } else if (hours < 18) {
    message = "下午";
  } else {
    message = "晚上";
  }
  return message;
};
</script>
<style lang="scss" scoped>
.login_container {
  width: 100%;
  height: 100vh;
  background: url("@/assets/images/login_icon.png") no-repeat;
  background-color: #0965ee;
  background-position: center left;
  .login_form {
    position: relative;
    width: 80%;
    top: 30vh;
    padding: 40px;

    h1 {
      color: white;
      font-size: 40px;
    }

    h2 {
      color: white;
      font-size: 20px;
      margin: 20px 0px;
    }

    .login_btn {
      width: 100%;
    }
  }
}
</style>
